package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: thoughtworker
 * Date: 10/14/13
 * Time: 10:30 AM
 * To change this template use File | Settings | File Templates.
 */
public class MainMenu implements MenuOptions{
    private final Map<String, Integer> bookList;
    private final Collection<Movie> movieList;
    private PrintStream printStream;
    private BufferedReader reader;
    private Map<Integer, MenuOptions> menuMap;
    private Collection<String> menuInstructions = Arrays.asList("Enter 1 to view the list of all books.", "Enter 2 to reserve books.",
            "Enter 3 to check your membership details.", "Enter 4 to view the details of all movies",
            "Enter 5 to log in.", "Enter q to exit.");

    public MainMenu(BufferedReader reader, PrintStream printStream, Map<String, Integer> bookList, Collection<Movie> movieList, Map<Integer, MenuOptions> menuMap) {
        this.reader = reader;
        this.printStream = printStream;
        this.bookList = bookList;
        this.movieList = movieList;
        this.menuMap = menuMap;
    }

    @Override
    public void execute() throws IOException {
        printWelcomeMessage();

        Menu mainMenu = new Menu(printStream, reader, menuInstructions);
        String userInput = mainMenu.promptUserInput();

        while (!userInput.equals("q")) {
            Integer userInputInt = Integer.parseInt(userInput);
            if (menuMap.containsKey(userInputInt)){
                menuMap.get(userInputInt).execute();
            } else {
                printInvalidOptionMessage();
            }
            userInput = mainMenu.promptUserInput();
        }
        printStream.println("You have exited.");

    }

    public void printWelcomeMessage(){
        printStream.println("Welcome to Bangalore Public Library");
    }

    public void printInvalidOptionMessage() {
        printStream.println("Select a valid option!!");
    }
}
